﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using fideeraSoft.Paneles;
using fideeraSoft.Entities;
using System.Windows.Forms;
using System.Drawing;
using fideeraSoft.Utils;
using System.Data;

namespace fideeraSoft.PanelesNuevosForms
{
    class PanelAddProveedorARemito:PanelGeneral
    {
        private Button btVolver;
        private TableLayoutPanel tlpBuscarProv;
        private Button btBuscarProvCuil;
        private Button btGuardarCambios;
        private TextBox tbBuscarProvCuil;
        private DataGridView dgvProveedores;

        int locationTlpBuscarProv;
        private TableLayoutPanel tlpBotones;
      
        private System.Data.SqlClient.SqlDataAdapter adapter;
        private DataSet ds;

        private Button btBuscarProvRaz;
        private TableLayoutPanel tlpBuscarProvRazon;
        private TextBox tbBuscarProvRazonS;
        int locationBtCambios;
        int locationTlpBuscarProvRazon;

        private Form formR;
      

        public Proveedor prov;

        public Label lbNombrePr;
        public PanelAddProveedorARemito()
        {
        
        }

        public PanelAddProveedorARemito(Form form,Label lbNombreProv,Proveedor proveedor)
        {
            this.formR = form;
            lbNombrePr = lbNombreProv;
            prov = proveedor;

            InitializeComponent();

            this.formatearPanelSegunForm(formR);
            this.formatearTableTitulo("Agregar Proveedor a Remito");

              string query = "SELECT * FROM Proveedor";
               adapter= SQLclass.obtenerAdapter(query);
               if (adapter != null)
               {

                   ds = new DataSet();
                   adapter.Fill(ds);
                   
                   dgvProveedores.DataSource = ds.Tables[0];


                   this.formatearDataGrid();
                   this.formatearTablaBuscarProv();
                   this.formatearTablaBuscarRazon();
                   this.formatearTablaBotones();

                   this.Controls.Add(dgvProveedores);
                   this.Controls.Add(tlpBuscarProv);
                   this.Controls.Add(tlpBuscarProvRazon);

                   this.Controls.Add(tlpBotones);
               }
               else 
               {                   
                   MessageBox.Show("NO HAY NINGUN PROVEEDOR CARGADO", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                   this.formR.Close();
               }


        }

        #region FORMATEAR

        private void formatearDataGrid()
        {
            dgvProveedores.AutoGenerateColumns = true;
            dgvProveedores.Size = new Size(this.Size.Width - 100, this.Size.Height - 400);
            dgvProveedores.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.Fill);
            dgvProveedores.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllHeaders);
            dgvProveedores.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvProveedores.BorderStyle = BorderStyle.Fixed3D;

            dgvProveedores.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, dgvProveedores.Size.Width), this.InitialRealPointVertical);
        }

        private void formatearTablaBuscarProv()
        {
            tlpBuscarProv.Controls.Add(btBuscarProvCuil);
            tlpBuscarProv.Controls.Add(tbBuscarProvCuil);


            tlpBuscarProv.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
            tlpBuscarProv.BackColor = Color.LightCyan;
            tlpBuscarProv.Size = new Size(400, tbBuscarProvCuil.Size.Height + 9);
            //tlpBuscarProv.Size = new Size(lbBuscarProvCuil.Size.Width + tbBuscarProvCuil.Size.Width + 10,tbBuscarProvCuil.Size.Height);
            int wid = MetodosGenerales.calcularCenterWidth(this.Size.Width, tlpBuscarProv.Size.Width);

            locationTlpBuscarProv = this.InitialRealPointVertical + dgvProveedores.Size.Height + 15;

            tlpBuscarProv.Location = new Point(wid, locationTlpBuscarProv);


        }

        private void formatearTablaBuscarRazon()
        {
            tlpBuscarProvRazon.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
            tlpBuscarProvRazon.BackColor = Color.LightCyan;
            tlpBuscarProvRazon.Size = new Size(400, tbBuscarProvRazonS.Size.Height + 9);

            locationTlpBuscarProvRazon = locationTlpBuscarProv + tlpBuscarProv.Size.Height;

            tlpBuscarProvRazon.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, tlpBuscarProvRazon.Size.Width), locationTlpBuscarProvRazon);

            tlpBuscarProvRazon.Controls.Add(btBuscarProvRaz);
            tlpBuscarProvRazon.Controls.Add(tbBuscarProvRazonS);


        }

        private void formatearTablaBotones()
        {

            
            
            locationBtCambios = locationTlpBuscarProvRazon + tlpBuscarProvRazon.Size.Height + 10;
            tlpBotones.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, tlpBotones.Size.Width), locationBtCambios);

            tlpBotones.Controls.Add(btVolver);
            tlpBotones.Controls.Add(btGuardarCambios);

        }

        #endregion

        #region INICIALIZAR COMPONENTES
        private void InitializeComponent()
        {
            this.dgvProveedores = new System.Windows.Forms.DataGridView();
            this.btVolver = new System.Windows.Forms.Button();
            this.tlpBuscarProv = new System.Windows.Forms.TableLayoutPanel();
            this.btBuscarProvCuil = new System.Windows.Forms.Button();
            this.btGuardarCambios = new System.Windows.Forms.Button();
            this.tbBuscarProvCuil = new System.Windows.Forms.TextBox();
            this.tlpBotones = new System.Windows.Forms.TableLayoutPanel();
            this.btBuscarProvRaz = new System.Windows.Forms.Button();
            this.tlpBuscarProvRazon = new System.Windows.Forms.TableLayoutPanel();
            this.tbBuscarProvRazonS = new System.Windows.Forms.TextBox();
            ((System.ComponentModel.ISupportInitialize)(this.dgvProveedores)).BeginInit();
            this.SuspendLayout();
            // 
            // dgvProveedores
            // 
            this.dgvProveedores.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dgvProveedores.Location = new System.Drawing.Point(0, 0);
            this.dgvProveedores.Name = "dgvProveedores";
            this.dgvProveedores.ReadOnly = true;
            this.dgvProveedores.Size = new System.Drawing.Size(240, 150);
            this.dgvProveedores.TabIndex = 0;
            this.dgvProveedores.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvProveedores_CellClick);
            this.dgvProveedores.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvProveedores_CellContentClick);
            // 
            // btVolver
            // 
            this.btVolver.BackColor = System.Drawing.Color.AliceBlue;
            this.btVolver.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btVolver.Location = new System.Drawing.Point(0, 0);
            this.btVolver.Margin = new System.Windows.Forms.Padding(12, 3, 3, 3);
            this.btVolver.Name = "btVolver";
            this.btVolver.Size = new System.Drawing.Size(75, 40);
            this.btVolver.TabIndex = 0;
            this.btVolver.Text = "Volver";
            this.btVolver.UseVisualStyleBackColor = false;
            this.btVolver.Click += new System.EventHandler(this.btVolver_Click);
            // 
            // tlpBuscarProv
            // 
            this.tlpBuscarProv.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBuscarProv.ColumnCount = 2;
            this.tlpBuscarProv.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarProv.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarProv.Location = new System.Drawing.Point(0, 0);
            this.tlpBuscarProv.Name = "tlpBuscarProv";
            this.tlpBuscarProv.RowCount = 1;
            this.tlpBuscarProv.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarProv.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarProv.Size = new System.Drawing.Size(400, 50);
            this.tlpBuscarProv.TabIndex = 0;
            // 
            // btBuscarProvCuil
            // 
            this.btBuscarProvCuil.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.btBuscarProvCuil.BackColor = System.Drawing.Color.AliceBlue;
            this.btBuscarProvCuil.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btBuscarProvCuil.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btBuscarProvCuil.Location = new System.Drawing.Point(0, 0);
            this.btBuscarProvCuil.Name = "btBuscarProvCuil";
            this.btBuscarProvCuil.Size = new System.Drawing.Size(75, 23);
            this.btBuscarProvCuil.TabIndex = 0;
            this.btBuscarProvCuil.Text = "Buscar Proveedor (Cuil)";
            this.btBuscarProvCuil.UseVisualStyleBackColor = false;
            this.btBuscarProvCuil.Click += new System.EventHandler(this.btBuscarProvCuil_Click);
            // 
            // btGuardarCambios
            // 
            this.btGuardarCambios.BackColor = System.Drawing.Color.AliceBlue;
            this.btGuardarCambios.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btGuardarCambios.Location = new System.Drawing.Point(0, 0);
            this.btGuardarCambios.Margin = new System.Windows.Forms.Padding(12, 3, 3, 3);
            this.btGuardarCambios.Name = "btGuardarCambios";
            this.btGuardarCambios.Size = new System.Drawing.Size(75, 40);
            this.btGuardarCambios.TabIndex = 0;
            this.btGuardarCambios.Text = "Guardar";
            this.btGuardarCambios.UseVisualStyleBackColor = false;
            this.btGuardarCambios.Click += new System.EventHandler(this.btGuardarCambios_Click);
            // 
            // tbBuscarProvCuil
            // 
            this.tbBuscarProvCuil.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbBuscarProvCuil.Location = new System.Drawing.Point(0, 0);
            this.tbBuscarProvCuil.MaxLength = 11;
            this.tbBuscarProvCuil.Name = "tbBuscarProvCuil";
            this.tbBuscarProvCuil.Size = new System.Drawing.Size(140, 30);
            this.tbBuscarProvCuil.TabIndex = 0;
            // 
            // tlpBotones
            // 
            this.tlpBotones.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.tlpBotones.BackColor = System.Drawing.Color.LightCyan;
            this.tlpBotones.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBotones.ColumnCount = 2;
            this.tlpBotones.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.Location = new System.Drawing.Point(0, 0);
            this.tlpBotones.Name = "tlpBotones";
            this.tlpBotones.RowCount = 1;
            this.tlpBotones.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlpBotones.Size = new System.Drawing.Size(200, 46);
            this.tlpBotones.TabIndex = 0;
            // 
            // btBuscarProvRaz
            // 
            this.btBuscarProvRaz.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.btBuscarProvRaz.BackColor = System.Drawing.Color.AliceBlue;
            this.btBuscarProvRaz.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btBuscarProvRaz.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btBuscarProvRaz.Location = new System.Drawing.Point(0, 0);
            this.btBuscarProvRaz.Name = "btBuscarProvRaz";
            this.btBuscarProvRaz.Size = new System.Drawing.Size(75, 23);
            this.btBuscarProvRaz.TabIndex = 0;
            this.btBuscarProvRaz.Text = "Buscar Prov. (Razón Social)";
            this.btBuscarProvRaz.UseVisualStyleBackColor = false;
            this.btBuscarProvRaz.Click += new System.EventHandler(this.btBuscarProvRaz_Click);
            // 
            // tlpBuscarProvRazon
            // 
            this.tlpBuscarProvRazon.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.tlpBuscarProvRazon.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBuscarProvRazon.ColumnCount = 2;
            this.tlpBuscarProvRazon.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarProvRazon.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarProvRazon.Location = new System.Drawing.Point(0, 0);
            this.tlpBuscarProvRazon.Name = "tlpBuscarProvRazon";
            this.tlpBuscarProvRazon.RowCount = 1;
            this.tlpBuscarProvRazon.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarProvRazon.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarProvRazon.Size = new System.Drawing.Size(400, 50);
            this.tlpBuscarProvRazon.TabIndex = 0;
            // 
            // tbBuscarProvRazonS
            // 
            this.tbBuscarProvRazonS.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
            this.tbBuscarProvRazonS.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbBuscarProvRazonS.Location = new System.Drawing.Point(0, 0);
            this.tbBuscarProvRazonS.MaxLength = 40;
            this.tbBuscarProvRazonS.Name = "tbBuscarProvRazonS";
            this.tbBuscarProvRazonS.Size = new System.Drawing.Size(200, 30);
            this.tbBuscarProvRazonS.TabIndex = 0;
            ((System.ComponentModel.ISupportInitialize)(this.dgvProveedores)).EndInit();
            this.ResumeLayout(false);

        }
        #endregion

        private void btBuscarProvCuil_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(tbBuscarProvCuil.Text) && (!(tbBuscarProvCuil.Text.Length < 11)))
            {
                string searchValue = tbBuscarProvCuil.Text;
                int inRowFinded = -1;

                try
                {
                    foreach (DataGridViewRow row in dgvProveedores.Rows)
                    {
                        if (row.Cells[3].Value.ToString().Equals(searchValue))
                        {
                            dgvProveedores.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                            row.Selected = true;
                            inRowFinded = row.Index;
                            //   MessageBox.Show("ENCONTRE: " + inRowFinded, "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            dgvProveedores.CurrentCell = row.Cells[0];
                            tbBuscarProvCuil.Clear();

                            break;
                        }
                    }

                }
                catch (Exception exc)
                {
                    //MessageBox.Show(exc.Message);     //DEBUG
                    //CLogger.Append("[btBuscarProvCuil_Click] Exception: " + exc, CLogger.ERROR);      //DEBUG
                    tbBuscarProvCuil.Clear();
                    MessageBox.Show("NO SE ENCONTRO EL PROVEEDOR", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            else
            {
                MessageBox.Show("NO SE INGRESO NINGÚN NUMERO DE CUIL \nÓ LA CANTIDAD DE DIGITOS ES INCORRECTA", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

            }
        }

        private void btBuscarProvRaz_Click(object sender, EventArgs e)
        {

            if (!string.IsNullOrWhiteSpace(tbBuscarProvRazonS.Text))
            {
                string searchValue = tbBuscarProvRazonS.Text;
                int inRowFinded = -1;
                try
                {
                    foreach (DataGridViewRow row in dgvProveedores.Rows)
                    {
                        if (row.Cells[1].Value.ToString().Equals(searchValue))
                        {
                            dgvProveedores.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

                            row.Selected = true;

                            inRowFinded = row.Index;
                            //MessageBox.Show("ENCONTRE: "+inRowFinded, "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            dgvProveedores.CurrentCell = row.Cells[0];
                            tbBuscarProvRazonS.Clear();
                            break;

                        }
                    }
                }
                catch (Exception exc)
                {
                    tbBuscarProvRazonS.Clear();
                    //MessageBox.Show(exc.Message); //DEBUG
                    //CLogger.Append("[btBuscarProvRaz_Click] Exception: " + exc, CLogger.ERROR);      //DEBUG
                    MessageBox.Show("NO SE ENCONTRO EL PROVEEDOR", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            else
            {
                MessageBox.Show("NO SE INGRESO RAZÓN SOCIAL", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

            }
        }

        private void btVolver_Click(object sender, EventArgs e)
        {
            this.formR.Close();
        }

        private void btGuardarCambios_Click(object sender, EventArgs e)
        {
            
            int rowIndex = dgvProveedores.CurrentCell.RowIndex;

            prov.IdProveedor= Convert.ToInt32(dgvProveedores.Rows[rowIndex].Cells[0].Value.ToString());
            prov.RazonSocial=dgvProveedores.Rows[rowIndex].Cells[1].Value.ToString();

            lbNombrePr.Text = prov.RazonSocial;
            //MessageBox.Show("RAZON SOCIAL: " +prov.RazonSocial, "Info", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

             formR.Close();
        }

        private void dgvProveedores_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            dgvProveedores.SelectionMode = DataGridViewSelectionMode.CellSelect;
        }

        private void dgvProveedores_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            dgvProveedores.SelectionMode = DataGridViewSelectionMode.CellSelect;
        }
    }
}
